package com.sky.controller.admin.user;

import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.vo.AddressBookVO;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBookVO
     * @return {@link Result}
     */
    @PostMapping
    public Result<?> save(@RequestBody AddressBookVO addressBookVO){
        addressBookService.save(addressBookVO);
        return Result.success();
    }

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return {@link Result}
     */
    @GetMapping("/list")
    public Result<List<AddressBookVO>> list(){
        List<AddressBookVO> addressBookVOList = addressBookService.list();
        return Result.success(addressBookVOList);
    }

    /**
     * 查询默认地址
     *
     * @return {@link Result}
     */
    @GetMapping("/default")
    public Result<AddressBookVO> defaultAddress(){
        AddressBookVO addressBookVO = addressBookService.defaultAddress();
        return Result.success(addressBookVO);
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<AddressBookVO> selectAddressById(@PathVariable Long id){
        AddressBookVO addressBookVO = addressBookService.selectAddressById(id);
        return Result.success(addressBookVO);
    }

    /**
     * 根据id修改地址
     *
     * @return {@link Result}
     */
    @PutMapping
    public Result<?> update(AddressBookVO addressBookVO){
        addressBookService.update(addressBookVO);
        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping
    public Result<?> delete(Long id){
        addressBookService.delete(id);
        return Result.success();
    }

    /**
     * 设置默认地址
     *
     * @param addressBookVO
     * @return {@link Result}
     */
    @PutMapping("/default")
    public Result setDefaultAddress(@RequestBody AddressBookVO addressBookVO){
        addressBookService.setDefaultAddress(addressBookVO);
        return Result.success();
    }

}
